﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class ABPRData : BaseFunction
    {
        
        public ABPRData()
            : base()
        {
           
        }
        public ABPRData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string ORDERNO,FDATE,EDATE;
            ORDERNO = dwABPOS["ORDERNO"].ToString().Trim();
            FDATE = dwABPOS["FDATE"].ToString().Trim();
            EDATE = dwABPOS["EDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
          
                #region
                or1.CommandText = " select araufkrs||'-'||to_number(arauflfd)||'-'||to_number(araufpos) AS ORDERNO, "
                    + " b.abpneudat,DECODE(ARAUFKRS,'FB','finishing bulk','FS','finishing sample','WB','weaving bulk','WS','weaving sample', "
                    + " 'DB','yarn dyeing bulk','DS','yarn dyeing sample','CB','winding bulk','CS','winding sample', "
                    + " 'FR','rework finishing','GR','rework greige','XB','repair','SB','sizing bulk', "
                    + " 'SS','sizing sample','UNKNOWN') AS ORDERTYPE, "
                    + " decode(b.abperlkz,'J','FINISHED',decode(b.abpgessta,'TE','TIMING','RU','WORKING', "
                    + " 'SP','BLOCK','FR','FINISHED',b.abpgessta)) AS STATUS, "
                    + " trim(b.abpart)||'-'||trim(b.abpfarbe)||'-'||trim(b.abpvar) as article, "
                    + " (select DISTINCT TRIM(D.LSNAME1) from ABFOLGE C,LIEFDAT D  "
                    + " WHERE C.ABFMANDANT = D.LSMANDANT AND C.ABFLIEFNR = D.LSLIEFNR "
                    + " AND C.ABFMANDANT = A.ARMANDANT AND C.ABFAUFKRS = A.ARAUFKRS "
                    + " AND C.ABFAUFLFD = A.ARAUFLFD AND C.ABFAUFPOS = A.ARAUFPOS "
                    + " AND C.ABFLIEFNR NOT IN ('        ','00000000')) AS FACTORY, "
                    + " arwert/1000 AS PRICE,armrkm1,armwert1*0.001 as wert1,armrkm2,armwert2*0.001 as wert2 "
                    + " from abpr a,abpos b "
                    + " where a.armandant =  b.abpmandant "
                    + " and a.araufkrs = b.abpaufkrs and a.arauflfd = b.abpauflfd "
                    + " and a.araufpos = b.abpaufpos "
                    + " and a.arafolge = '000' and a.araufkrs NOT IN ('PB','  ','QP') "
                    + " AND (araufkrs||'-'||to_number(arauflfd)||'-'||to_number(araufpos) = '" + ORDERNO + "'  "
                    + "  OR '" + ORDERNO + "' IS NULL )  " 
                    + " and b.abpneudat >= '" + FDATE + "' AND b.ABPNEUDAT <= '" + EDATE + "' "                  
                    ;

                #endregion
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "ABPR";

                    byte[] b;
                    string value;
                    string Column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                    }
                   
                }
                setResult.AcceptChanges();
                set4Return.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        
        public DataSet BuildGBBEW(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}